package com.bancomer.batch.validators.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.bancomer.batch.Constants;
import com.bancomer.batch.MapConstants;
import com.bancomer.batch.pojo.Alert;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.ValidationResult;
import com.bancomer.batch.util.FilesWriter;
import com.bancomer.batch.validators.GeneralValidator;

public class LayoutValidator implements GeneralValidator {
	private Map<String,String> loads = new HashMap<String,String>();
	
	public ValidationResult leerLayout(FileHelper file, Alert alert){
		ValidationResult result = new ValidationResult();
		FileReader input;
		try {
			MapConstants properties = MapConstants.getInstance(alert.getProperties().getProperty("constantsFile"));
			final Properties alertConstants = properties.getConstants();
			File inputFile = new File(file.getLocalPath()+file.getName());
			input = new FileReader(inputFile);
			BufferedReader buffer = new BufferedReader(input);
			StringBuffer load = new StringBuffer();
			String line;
			StringBuffer loadPositions = new StringBuffer();
			StringBuffer loadNames = new StringBuffer();
			String[] columnValues;
			String[] columnPositions;
			String columnName;
			String beginPosition;
			String finalPosition;
			int positionInt;
			int loadsCounter = 1;
			line = buffer.readLine();
			while(line!=null){
				String fileColumns = line.split("=")[1];
				String[] columns = fileColumns.split(";");
				load.append("com.bancomer.pig.functions.IndexOfLoader(");
				for(int i=0;i<columns.length;i++){
					columnValues = columns[i].split(",");
					columnPositions = columnValues[0].split("-");
					columnName = columnValues[1];
					positionInt = Integer.parseInt(columnPositions[0])-1;
					beginPosition = String.valueOf(positionInt);
					finalPosition = columnPositions[1];
					loadPositions.append("'"+beginPosition+"-"+finalPosition+"', ");
					loadNames.append(columnName+":chararray, ");
				}
				loadPositions.delete(loadPositions.length()-2,loadPositions.length());
				loadNames.delete(loadNames.length()-2,loadNames.length());
				load.append(loadPositions.toString()+") as ("+loadNames.toString()+")");
				loads.put("load"+loadsCounter, load.toString());
				loadPositions.delete(0,loadPositions.length());
				loadNames.delete(0,loadNames.length());
				load.delete(0,load.length());
				loadsCounter++;
				line = buffer.readLine();
			}
			buffer.close();
			FileHelper outputFile = new FileHelper();
			for(FileHelper parameterFile:alert.getOutputFiles()){
				if(parameterFile.getType().equals(alertConstants.getProperty("FILE_PIG_PROPERTIES"))){
					outputFile = parameterFile;
					break;
				}
			}
			//borra archivo para crearlo por primera vez al ejecutarse la alerta
			File f = new File(outputFile.getLocalPath()+outputFile.getName());
			if(f.exists()==true){
				f.delete();
			}
			FilesWriter.writePropertiesFile(outputFile, loads);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			result.setMessage("Class: LayoutClassValidator - "+e.getMessage());
			result.setSucces(false);
		}
		return result;
	}

	@Override
	public ValidationResult executeValidation(FileHelper fileHelper,
			Object... obj) {
		ValidationResult result = new ValidationResult();
		try {
			result = leerLayout(fileHelper,(Alert)obj[0]);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setMessage(e.getMessage());
			result.setSucces(false);
		}
		return result;
	}
}
